<?php
namespace app\index\controller;
use \think\Request;
use \think\Db;
use \think\Session;
class Login extends Common
{
    public function index()
    {
       /* $info = Request::instance()->header();
		echo $info['accept'].'<br />';
		echo $info['accept-encoding'].'<br />';
		echo $info['user-agent'].'<br />';
        $bin = get_client_ip6();
        echo $bin;*/
    }
    /*
     * 登陆
     * */
    public function login(){
        return $this -> fetch();
    }
    public function doLogin(){
        $param = Request::instance() ->instance() -> Param();
        $member = Db::table('member') -> where('member_phone|member_mail',$param['member_title']) -> find();
        if($member){
            if(md5($param['member_pass']) != $member['member_pass']){
                return array(
                    'error' => 1,
                    'msg' => '账号或密码错误'
                );
            }else{
                $time = time();
                $ip = get_client_ip6();
                Db::table('member')
                    -> where('member_phone|member_mail',$param['member_title'])
                    -> update(array('member_ndate'=>$time,'member_nip'=>$ip));
                Session::set('member_id',$member['member_id']);
                return array(
                    'error' => 0,
                    'msg' => '登陆成功',
                    'url' => '/'
                );
            }
        }else{
            return array(
                'error' => 1,
                'msg' => '账号或密码错误'
            );
        }
    }
    /*
     * 注册
     * */
    public function register(){
        return $this->fetch();
    }
    public function doRegister(){
        $param = Request::instance() -> Param();
        if(array_key_exists('member_mail', $param)){
            $data['member_mail'] = $param['member_mail'];
            $data['member_pass'] = $param['member_pass'];
            if(!checkEmail($data['member_mail'])){
                return array(
                    'error' => 1,
                    'msg'   => '邮箱格式不正确'
                );
            }
            if($data['member_pass'] != $param['repass']){
                return array(
                    'error' => 1,
                    'msg'  => '两次密码输入不一致'
                );
            }
            $data['member_pass'] = md5($data['member_pass']);
            $data['member_date'] = time();
            $data['member_mail_auth'] = 1;
            $data['member_ip'] = get_client_ip6();
            $res = Db::table('member') -> insert($data);
            if($res){
                return array(
                    'error' => 0,
                    'msg'   => '注册成功',
                    'url'   => '/Index/Login/login'
                );
            }else{
                return array(
                    'error' => 1,
                    'msg'   => '注册失败'
                );
            }
        }else{
            $data['member_phone'] = $param['member_phone'];
            $data['member_pass'] = $param['member_pass'];
            $check = Db::table('member_verify') -> where(array('send_target'=>$data['member_phone'],'send_state'=>0))->find();
            if($check['send_code'] != $param['sms']){
                return array(
                    'error' => 1,
                    'msg'   => '手机验证码不正确'
                );
            }
            $check = Db::table('member_verify') -> where(array('send_target'=>$data['member_phone'],'send_state'=>0))->update(array('send_state'=>1));
            if(!checkMobile($data['member_phone'])){
                return array(
                    'error' => 1,
                    'msg'   => '手机号码不正确'
                );
            }
            if($data['member_pass'] != $param['repass']){
                return array(
                    'error' => 1,
                    'msg'  => '两次密码输入不一致'
                );
            }
            $data['member_pass'] = md5($data['member_pass']);
            $data['member_date'] = time();
            $data['member_phone_auth'] = 1;
            $data['member_ip'] = get_client_ip6();
            $res = Db::table('member') -> insert($data);
            if($res){
                return array(
                    'error' => 0,
                    'msg'   => '注册成功',
                    'url'   => '/Index/Login/login'
                );
            }else{
                return array(
                    'error' => 1,
                    'msg'   => '注册失败'
                );
            }
        }

    }
    /*
	 * 发送验证码
	 */
    public function getSms(){

        $param = Request::instance() -> Param();
        $mobile = $param['phone'];

        if(!checkMobile($mobile)){
            $this->ajaxReturn(array(
                'error' => 1,
                'msg'   => '手机号不正确'
            ));
        }
        $ret = Db::table('member')->where(array('member_phone'=>$mobile,'member_phone_auth'=>'1'))->find();
        if($ret){
            return array(
                'error' => 1,
                'msg'   => '此手机已绑定平台账户'
            );
        }
        $code = rand(100000,999999);
        $now_time = time();
        $result = $this->check_number($mobile,$now_time,1);
        if(!$result){
            $this->ajaxReturn(array(
                'error' => 1,
                'msg'   => "超出当日可发<br>送验证码次数"
            ));
        }
        // 添加验证码记录
        $data = array();
        $data["send_code"]   = $code;
        $data["send_target"] = $mobile;
        $data["send_time"]   = $now_time;
        $data["send_type"]   = 1;
        $data["send_state"]  = 0;
        $result = Db::table('member_verify') -> insert($data);
        /*$mailTmpl = M("mailTemplates")->where(array("code"=>"member_register"))->find();
        $params = array();
        $params['site_name']    = C('SMS_SIGN');
        $params['send_time']    = date("Y-m-d H:i:s",$now_time);
        $params['verify_code']  = $code;
        $content = ncReplaceText($mailTmpl['content'],$params);*/
        if($result){
            return array(
                "error" => 0,
                "msg"   => "发送成功，请在<br>30分钟内完成验证",
            );
        } else {
            return array(
                "error" => 1,
                "msg"   => "发送失败"
            );
        }

    }

    /*
     * 检测是否已超出当天可发送验证码次数
     */
    public function check_number($mobile,$send_time,$type){

        $start = mktime(0,0,0,date("m",$send_time),date("d",$send_time),date("Y",$send_time));
        $end = mktime(23,59,59,date("m",$send_time),date("d",$send_time),date("Y",$send_time));
        $result = Db::table('member_verify')->where(array('send_type'=>$type,"send_target"=>$mobile,"send_time"=>array("BETWEEN",array($start,$end))))->count();
        if($result > 6){
            return false;
        }else{
            return true;
        }
    }

}
